﻿Imports System.IO

Public Class ExploradorCarpetas

    ''' <summary>
    ''' Loads all the subnodes from the original node, sended as a parameter.
    ''' </summary>
    ''' <param name="node">First node</param>
    ''' <remarks></remarks>
    Public Sub RellenarSubNodos(ByVal node As TreeNode)
        Try
            Dim dirs As New DirectoryInfo(node.FullPath)
            For Each dir As DirectoryInfo In dirs.GetDirectories()
                Dim myNode As New TreeNode(dir.Name)
                node.Nodes.Add(myNode)
                myNode.Nodes.Add("*")
            Next
        Catch ex As Exception
            MessageBox.Show("Some error found in RellenarSubNodos")
        End Try
    End Sub

    ''' <summary>
    ''' Loads local directory in a TreeView from a path.
    ''' </summary>
    ''' <param name="currentLocalPath">Path inicial desde el que se van a cargar las subcarpetas</param>
    ''' <param name="listStyle">Parámetro bastante opcional, ya que solo selecciona la forma de mostrar la lista.</param>
    ''' <remarks></remarks>
    Public Sub LoadLocal(ByVal currentLocalPath As String, ByVal listStyle As Integer)
        Dim localDirectory As String = currentLocalPath
        If IO.Directory.Exists(localDirectory) Then
            Dim rootnode As New TreeNode(localDirectory)
            frmPrincipal.treeCarpetas.Nodes.Add(rootnode)
            Me.RellenarSubNodos(rootnode)
            frmPrincipal.treeCarpetas.Nodes(0).Expand()
        End If
    End Sub

    ''' <summary>
    ''' Loads a file list from a specific path into a ListView (lstArchivos
    ''' </summary>
    ''' <param name="miPath">Path where the files are</param>
    ''' <remarks></remarks>
    Public Sub LoadFileList(ByVal miPath As String)
        Try
            Dim files As String() = Directory.GetFiles(miPath)
            frmPrincipal.lstArchivos.Items.Clear()

            For Each item As String In files
                Dim extension As String = Path.GetExtension(item)

                If Not frmPrincipal.imlSmall.Images.ContainsKey(extension) Then
                    Dim iconSmall As Icon = ExtractorIconos.ObtenerIconos(item, True)
                    frmPrincipal.imlSmall.Images.Add(extension, iconSmall)
                    Dim iconLarge As Icon = ExtractorIconos.ObtenerIconos(item, False)
                    frmPrincipal.imlLarge.Images.Add(extension, iconLarge)
                End If

                Dim listviewItem As New ListViewItem(Path.GetFileName(item), extension)
                frmPrincipal.lstArchivos.Items.Add(listviewItem)
            Next
        Catch ex As Exception
            MessageBox.Show("Some exception or error found loading the file list.")
        End Try
    End Sub
End Class
